﻿using System;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using OurSite.Business.Repositories;

namespace OurSite.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("*.html|js|css|gif|jpg|jpeg|png|swf");

            routes.MapRoute(
                "Event", // Route name
                "Photos.aspx/Event/{eventCode}/{eventName}", // URL with parameters
                new { controller = "Photos", action = "Event", eventCode = "", eventName = "" } // Parameter defaults
            );

            routes.MapRoute(
                "Place", // Route name
                "Photos.aspx/Place/{locationType}/{locationName}", // URL with parameters
                new { controller = "Photos", action = "Place", locationType = "", locationName = "" } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}.aspx/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );           

            routes.MapRoute(
                "Root",
                "",
                new { controller = "Home", action = "Index", id = "" }
                );

            //routes.MapRoute(
            //    "ByLocation", // Route name
            //    "{controller}.aspx/{action}/{type}/{value}", // URL with parameters
            //    new { controller = "Photos", action = "ByLocation", type = UrlParameter.Optional , value = UrlParameter.Optional} // Parameter defaults
            //);


        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            NHibernateSessionManager.Instance.Initialize();
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            // Get the authentication cookie
            string cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = Context.Request.Cookies[cookieName];

            if (authCookie == null)
            {
                return; //no cookie? Was not authenticated.
            }

            // Get authentication ticket and rebuild the principal & identity
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

            string[] roles = authTicket.UserData.Split(new Char[] { '|' });

            //Rebuild Identity:
            GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);

            //Rebuild Principal:
            GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity, roles);

            //Stick it on the thread:
            Context.User = userPrincipal;
        }
    }
}